<?php

function getDbConnection($dsn, $user, $pass) {
        $ret=null;
        try{
                $ret=new PDO($dsn, $user, $pass);
        }
        catch(PDOException $e){
                echo "Erreur de connexion vers $dsn: ".$e->getMessage();
                $ret=null;
                throw new Exception($e);
        }
		print_r($ret->errorInfo());
        if ($ret==NULL) throw new Exception("NULL connection");
        
        return $ret;
}


function getNbEnregistrements($dbh, $tblName) {
        $nb=0;
        try  {
                $sth = $dbh->prepare('SELECT COUNT(*) FROM '.$tblName);
                $sth->execute();
                $results=$sth->fetchAll(PDO::FETCH_COLUMN, 0);
                $nb=$results[0];
        } catch (Exception $e) {
                echo "$e";
        }
        $sth->closeCursor();
        return $nb;
}

function getSqlResultat($dhn, $sql, $mode=PDO::FETCH_BOTH) {
	$sth = $dbh->prepare($sql);
	$sth->execute();
	if ($sth->errorCode()=== '00000') {
			$result = $sth->fetchAll($mode==NULL?:$mode);
	} else {
		print_r($sth->errorInfo());
	}
	
	$sth->closeCursor();
	return $result;
}

function getSqlAssocResultat($dhn, $sql) {
	return getSqlResultat($dhn, $sql, PDO::FETCH_ASSOC);
}

function getSqlObjetResultat($dhn, $sql) {
	return getSqlResultat($dhn, $sql, PDO::FETCH_OBJ);
}

?>
